
cc.Class({
    extends: require('wy-Component'),

    properties: {
        mainLabel : cc.Label,
        childLabel :cc.Label,
        healthLabel : cc.Label,
        btn:[cc.Sprite],
        now:[cc.Label],
        after:[cc.Label],
        powerValue:require('JumpText'),
        font:[cc.Font],
        try:cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.main=global.Data.mainArmsLevel;
        this.child=global.Data.childArm;
        this.speed=global.Data.speedLevel;

        // if(!!global.usedFullLevel){
        //     this.try.children[0].children[0].active=false;
        //     this.try.children[0].children[1].getComponent(cc.Label).string='Max'
        // }
        // this.try.active=!!global.tryFullLevel
    },

    start () {
       this.oldValue=0;
        this.arr=[cc.find('main/btnLevelUp/main',this.node),cc.find('health/btnLevelUp/health',this.node),cc.find('child/btnLevelUp/child',this.node)]
        var self = this;
        this.btn[0].node.on(cc.Node.EventType.TOUCH_END,function(){
            self.onClick(null,'mainArms')
        },this)
        this.btn[1].node.on(cc.Node.EventType.TOUCH_END,function(){
            self.onClick(null,'health')
        },this)
        this.btn[2].node.on(cc.Node.EventType.TOUCH_END,function(){
            self.onClick(null,'childArms')
        },this); 

        this.schedule(function () {
            this.refershUI();
        }.bind(this), 1)

        //当前等级
        this.mainLevel=cc.find('main/showLevel/level',this.node).getComponent(cc.Label);
        this.healthLevel=cc.find('health/showLevel/level',this.node).getComponent(cc.Label);
        this.childLevel=cc.find('child/showLevel/level',this.node).getComponent(cc.Label);
    },

    refershUI(){
        /*var data = global.Data.plane

        var mainData=data.nextMainArmsGoldCoin;
        var childData=data.nextChildArmsGoldCoin;
        var healthData=data.nextSpeedGoldCoin;

        this.mainLabel.string = global.Common.getKV(mainData);
        this.childLabel.string = global.Common.getKV(childData);
        this.healthLabel.string = global.Common.getKV(healthData);

        this.mainLevel.string=data.mainArmsLevel;
        this.healthLevel.string=data.speedLevel;
        this.childLevel.string=data.childArm[data.currentChildArm];*/

        this.mainLevel.string=this.main;
        this.healthLevel.string=this.speed;
        this.childLevel.string=this.child;

        this.mainLabel.string = global.Common.getKV(Math.ceil(200*(this.main+1)*Math.pow(1.2,this.main)));
        this.childLabel.string = global.Common.getKV(Math.ceil(200*(this.child+1)*Math.pow(1.2,this.child)));
        this.healthLabel.string = global.Common.getKV(Math.ceil(200*(this.speed+1)*Math.pow(1.2,this.speed)));
        
        this.btnColor(0)
        this.btnColor(1)
        this.btnColor(2);
        this.updateTips();
        
    },

    updateTips(){
        // var data = global.Data.plane
        this.now[0].string=parseInt(100*(this.main*0.1+1))
        this.now[1].string=parseInt(100*(this.speed*0.02+1))
        this.now[2].string=parseInt(100*(this.child*0.1+1))

        this.after[0].string=parseInt(100*(this.main*0.1+1.2))
        this.after[1].string=parseInt(100*(this.speed*0.02+1.01))
        this.after[2].string=parseInt(100*(this.child*0.1+1.2))

        this.nowValue=(parseInt(100*(this.main*0.1+1))+parseInt(100*(this.speed*0.02+1))+parseInt(80*(this.child*0.1+1)))*100

        this.powerValue.setText(this.oldValue,this.nowValue);

        this.oldValue=this.nowValue;
    },


    btnColor(data1){
        // var data = global.Data.plane;
        this.btn[data1].setState(0)
        cc.find('jinbi',this.btn[data1].node).getComponent(cc.Sprite).setState(0)
        var coin=[
            Math.ceil(200*(this.main+1)*Math.pow(1.2,this.main)), 
            Math.ceil(200*(this.speed+1)*Math.pow(1.2,this.speed)),
            Math.ceil(200*(this.child+1)*Math.pow(1.2,this.child))
        ];
        if(global.Data.goldCoin<coin[data1]){
            this.arr[data1].getComponent(cc.Label).font=this.font[1];
            this.btn[data1].setState(1)
            cc.find('jinbi',this.btn[data1].node).getComponent(cc.Sprite).setState(1)
        }
    },

    onClick(e,d){

        var node=this.node.parent
        var upgradePanel=cc.find('upgradePanel',node);
        var bottomFriend=cc.find('friendLinkBottom',node)
        var jiantou=cc.find('upgradePanel/jiantoukuang/jiantou',node);
        if(jiantou.scaleY==1){
            jiantou.scaleY=-1;
            upgradePanel.runAction(cc.moveBy(0.2,cc.v2(0,bottomFriend.height)))
        }
            
        // var data = global.Data.plane
        var useCoin=0;
        var propName='';
        if(d=='mainArms'){
            propName='mainArmsLevel';
            useCoin=Math.ceil(200*(this.main+1)*Math.pow(1.2,this.main))
        }else if(d=='health'){
            useCoin= Math.ceil(200*(this.speed+1)*Math.pow(1.2,this.speed))
            propName='speedLevel'
        }else if(d=='childArms'){
            propName='childArm'
            useCoin=Math.ceil(200*(this.child+1)*Math.pow(1.2,this.child))
        }
        SoundMgr.playButtonEffect()
        if(global.Data.goldCoin < useCoin){
            var add=global.EJMgr.pushUI("SelectPlane/addPowerCoin")
            add.getComponent('addPowerCoin').setOptions(1)
            return
        }
        var self = this;
        this.httpGet('/game/gameUser',"attrLevelUp",
            {
                propName: propName,
                goldCoinCount: useCoin
            },function(err,res){
                if(!err){
                    global.Data.goldCoin -= useCoin;
                    if(d=='mainArms'){
                        self.main++;
                        global.Data.mainArmsLevel++;
                    }else if(d=='health'){
                        self.speed++
                        global.Data.speedLevel++;
                    }else if(d=='childArms'){
                        self.child++;
                        global.Data.childArm++;
                    }
                    self.refershUI();
                    global.EJMgr.showToast('升级成功');
                }
            }
        );

    },
    
    testFullLevel(){
        cc.log('满级适用')

        var self=this
        var options={
            adUnitId:'adunit-6ca9f0f26ddc27cd',
            stopCb:function(){
                global.EJMgr.showToast('视频未看完，不能获得奖励')
            },
            successCb:function(){
                global.usedFullLevel=true;
                self.try.children[0].children[0].active=false;
                self.try.children[0].children[1].getComponent(cc.Label).string='Max'
            }
        }
        global.WechatShare.videoOrShare(options)


    },

    // update (dt) {},
});
